define(['moment'], function (moment) {
    return ['$state', '$scope', '$rootScope', '$filter', '$stateParams', 'config', 'MMWService', '$mdDialog', '$timeout','$calculater', '$datePicker',
        function ($state, $scope, $rootScope, $filter, $stateParams, config, $MMWService, $mdDialog, $timeout, $calculater, $datePicker) {

            $scope.init = function () {
                $scope.opmList = {
                    user_no: '',
                    user_name: '',
                    report_reason: '',
                    type_no: '', // 0: 按數量 ; 1: 按時間
                    type_name: '',
                    start_date: null,
                    start_time: null,
                    end_date: null,
                    end_time: null,
                    output: 0,
                    is_modify: null, // 單價是否能修改 → 0(不能) ; 1(能)
                    unit_price: 0,
                    total_amount: 0,
                    whether_to_deduct: false, // 是否扣減按鈕
                    is_deduct: 0, // 是否扣減 → 0(否) ; 1(是)
                    product_no: '',
                    product_name: '',
                    product_version:'',
                    wo_no: '',
                    lot_no: '',
                    op_no: '',
                    description: ''
                };
                defaultDateTime();
            };

            // 預設開始(結束)日期時間
            function defaultDateTime(){
                var nowDateTime = moment().format('YYYY-MM-DD HH:mm:ss');
                $scope.opmList.start_date = nowDateTime.split(' ')[0];
                $scope.opmList.start_time = nowDateTime.split(' ')[1];
                $scope.opmList.end_date = nowDateTime.split(' ')[0];
                $scope.opmList.end_time = nowDateTime.split(' ')[1];
            };

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };
            
            // 取得用戶編號資訊
            $scope.loadUserNo = function(){
                $MMWService.showQueryListPNIG({
                    page_mode: 'opm_user_no_info_get',
                    pageRecord: 50,
                    condition: [
                        {name:$filter('translate')('OPM01.queryList.user_no'), column_name:'UserNo', query_mode:'2', value: $scope.opmList.user_no}
                    ],
                    header: [
                        {label: $filter('translate')('OPM01.queryList.user_no'), code:'user_no'},
                        {label: $filter('translate')('OPM01.queryList.user_name'), code:'user_name'}
                    ],
                    setCondition:function(conditions){
                        conditions.push(
                            {column_name:'IssueState', query_mode:'0', value:'2', merge_condition_model:'AND' }
                        );
                        return conditions;
                    },
                    selectCode: $scope.opmList.user_no || '',
                    confirm : function(selectItem){
                        $scope.opmList.user_no = selectItem.user_no;
                        $scope.opmList.user_name = selectItem.user_name;
                    }
                });
            };

            // 取得報工原因資訊
            $scope.loadReportReason = function(){
                $scope.$root.showQueryList({
                    key:'report_reason',
                    pageRecord: 50,
                    header : [
                        {label: $filter('translate')('OPM01.queryList.report_reason'), code:'report_reason'},
                        {label: $filter('translate')('OPM01.queryList.type'), code:'type_name'},
                        {label: $filter('translate')('OPM01.queryList.unit_price'), code:'unit_price'},
                        {label: $filter('translate')('OPM01.queryList.description'), code:'description'}
                    ],
                    query: function(conditions, feedback){
                        $MMWService.pageNumberInfoGet({
                            page_mode : 'opm_report_reason_info_get',
                            query_condition_info:[]
                        }, function(itemList){
                            if(itemList && itemList.length > 0)
                                feedback(itemList);
                            else 
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                        });
                    },
                    selectCode: $scope.opmList.report_reason || '',
                    confirm: function(selectItem, dialog){
                        dialog.hide();
                        $scope.opmList.report_reason = selectItem.report_reason;
                        $scope.opmList.type_no = selectItem.type_no;
                        $scope.opmList.type_name = selectItem.type_name;
                        $scope.opmList.unit_price = selectItem.unit_price;
                        $scope.opmList.is_modify = selectItem.is_modify;
                        $scope.opmList.description = selectItem.description;                        
                        $scope.opmList.total_amount = ($scope.opmList.unit_price * $scope.opmList.output);
                    }
                });
            };

            // 更換開始日期
            $scope.changeStartDate = function(){
                $datePicker.open($scope.opmList.start_date,
                function(dialog){
                    if(moment(dialog.datePacker.pickDate).isBefore($scope.opmList.end_date)
                    || moment(dialog.datePacker.pickDate).isSame($scope.opmList.end_date))
                    {
                        $scope.opmList.start_date = moment(dialog.datePacker.pickDate).format('YYYY-MM-DD');
                        dialog.hide();
                    }  
                    else 
                        $scope.$root.showAlert($filter('translate')('OPM01.msg.dateTime.column01')); // 開始日期不能大於結束日期！
                },
                function(dialog){
                    dialog.hide();
                },{});
            };

            // 更換結束日期
            $scope.changeEndDate = function(){
                $datePicker.open($scope.opmList.end_date,
                function(dialog){
                    if(moment(dialog.datePacker.pickDate).isAfter($scope.opmList.start_date)
                    || moment(dialog.datePacker.pickDate).isSame($scope.opmList.start_date))
                    {
                        $scope.opmList.end_date = moment(dialog.datePacker.pickDate).format('YYYY-MM-DD');
                        dialog.hide();
                    }  
                    else
                        $scope.$root.showAlert($filter('translate')('OPM01.msg.dateTime.column02')); // 結束日期不能小於開始日期！
                },
                function(dialog){
                    dialog.hide();
                },{});
            };

            // 更換開始時間
            $scope.changeStartTime = function(event){
                changeTime(event, {
                    date: $scope.opmList.start_date,
                    time: $scope.opmList.start_time,
                    compare_dateTime: $scope.opmList.end_date + ' ' + $scope.opmList.end_time,
                    type: 'start'
                });
            };

            // 更換結束時間
            $scope.changeEndTime = function(event){
                changeTime(event, {
                    date: $scope.opmList.end_date,
                    time: $scope.opmList.end_time,
                    compare_dateTime: $scope.opmList.start_date + ' ' + $scope.opmList.start_time,
                    type: 'end'
                });
            };

            // 更換時間
            function changeTime(event, options){
                var target = event.currentTarget;
                angular.element(target).addClass('is-focus');
                $calculater.open(function(timeDisplay,dialog){
                    angular.element(target).removeClass('is-focus');
                    var hour = timeDisplay.hour,
                        min = timeDisplay.minute,
                        sec = timeDisplay.second,
                        select_dateTime = moment(String(options.date + ' ' + hour + ':' + min + ':' +sec)).format('YYYY-MM-DD HH:mm:ss'),
                        compare_dateTime = moment(String(options.compare_dateTime)).format('YYYY-MM-DD HH:mm:ss');
                    if((0 <= (+hour) && (+hour) < 24) && (0 <= (+min) && (+min) < 60) && (0 <= (+sec) && (+sec) < 60))
                    {
                        if(options.type == 'start')
                        {
                            if(moment(select_dateTime).isBefore(compare_dateTime)
                            || moment(select_dateTime).isSame(compare_dateTime))
                            {
                                $scope.opmList.start_time = select_dateTime.split(' ')[1];
                                dialog.hide();
                            }
                            else
                                $scope.$root.showAlert($filter('translate')('OPM01.msg.dateTime.column03')); // 開始時間不能大於結束時間！
                        }
                        else
                        {
                            if(moment(select_dateTime).isAfter(compare_dateTime)
                            || moment(select_dateTime).isSame(compare_dateTime))
                            {
                                $scope.opmList.end_time = select_dateTime.split(' ')[1];
                                dialog.hide();
                            }
                            else
                                $scope.$root.showAlert($filter('translate')('OPM01.msg.dateTime.column04')); // 結束時間不能小於開始時間！
                        }
                    }
                    else
                        $scope.$root.showAlert($filter('translate')('common.msg.time_format_error'));
                },
                function(dialog){
                    angular.element(target).removeClass('is-focus');
                    dialog.hide();
                },
                {
                    defaultNum : options.time,
                    title:$filter('translate')('common.msg.select_time'),
                    decimal : false,
                    type:'date'
                });
            };   

            // 更換產出量數量
            $scope.modifyOutputValue = function(event){
                var target = event.currentTarget;
                angular.element(target).addClass('is-focus');
                $calculater.open(function (num, dialog) {
                    $scope.opmList.output = num;
                    $scope.opmList.total_amount = $calculater.decimalMutiple(+($scope.opmList.unit_price), num);
                    dialog.hide();
                },
                function (dialog) {
                    angular.element(target).removeClass('is-focus');
                    dialog.hide();
                },
                {
                    defaultNum: $scope.opmList.output,
                    decimal: true
                });
            };

            // 更換單價數量
            $scope.modifyUnitPriceValue = function(event){
                var target = event.currentTarget;
                angular.element(target).addClass('is-focus');
                $calculater.open(function (num, dialog) {
                    $scope.opmList.unit_price = num;
                    $scope.opmList.total_amount = (num * $scope.opmList.output);
                    dialog.hide();
                },
                function (dialog) {
                    angular.element(target).removeClass('is-focus');
                    dialog.hide();
                },
                {
                    defaultNum: $scope.opmList.unit_price,
                    decimal: false
                });
            };

            // 是否扣減按鈕 → 0(否) ; 1(是)
            $scope.whetherToDeduct = function(){
                if(!$scope.opmList.whether_to_deduct)
                {
                    $scope.opmList.whether_to_deduct = true;
                    $scope.opmList.is_deduct = 1;
                }
                else
                {
                    $scope.opmList.whether_to_deduct = false;
                    $scope.opmList.is_deduct = 0;
                };
            };

            // 取得產品編號資訊
            $scope.loadProductNo = function(){
                $MMWService.showQueryListPNIG({
                    page_mode: 'opm_product_no_info_get',
                    pageRecord: 50,
                    condition: [
                        {name:$filter('translate')('OPM01.queryList.product_no'), column_name:'ProductNo', query_mode:'2', value: $scope.opmList.product_no}
                    ],
                    header: [
                        {label: $filter('translate')('OPM01.queryList.product_no'), code:'product_no'},
                        {label: $filter('translate')('OPM01.queryList.product_name'), code:'product_name'},
                        {label: $filter('translate')('OPM01.queryList.product_version'), code:'product_version'}
                    ],
                    setCondition:function(conditions){
                        conditions.push(
                            {column_name:'IssueState', query_mode:'0', value:'2', merge_condition_model:'AND' },
                            {column_name:'CurVersion', query_mode:'0', value:'1', merge_condition_model:'AND' },
                        );
                        return conditions;
                    },
                    selectCode: $scope.opmList.product_no || '',
                    confirm : function(selectItem){
                        $scope.opmList.product_no = selectItem.product_no;
                        $scope.opmList.product_name = selectItem.product_name;
                        $scope.opmList.product_version = selectItem.product_version;
                    }
                });
            };

            // 取得工單號資訊
            $scope.loadWoNo = function(){
                $MMWService.showQueryListPNIG({
                    page_mode: 'opm_wo_no_info_get',
                    pageRecord: 50,
                    condition: [
                        {name:$filter('translate')('OPM01.queryList.wo_no'), column_name:'A.MONo', query_mode:'2', value: $scope.opmList.wo_no}
                    ],
                    header: [
                        {label: $filter('translate')('OPM01.queryList.wo_no'), code:'wo_no'},
                        {label: $filter('translate')('OPM01.queryList.product_no'), code:'product_no'},
                        {label: $filter('translate')('OPM01.queryList.product_name'), code:'product_name'}
                    ],
                    setCondition:function(conditions){
                        if($scope.opmList.product_no){
                            conditions.push(
                                {column_name:'A.PRODUCTNO', query_mode:'2', value: $scope.opmList.product_no, merge_condition_model:'AND'}
                            );
                        }
                        return conditions;
                    },
                    selectCode: $scope.opmList.wo_no || '',
                    confirm : function(selectItem){
                        $scope.opmList.wo_no = selectItem.wo_no;
                    }
                });
            };

            // 取得生產批號資訊
            $scope.loadLotNo = function(){
                $MMWService.showQueryListPNIG({
                    page_mode: 'opm_lot_no_info_get',
                    pageRecord: 50,
                    condition: [
                        {name:$filter('translate')('OPM01.queryList.lot_no'), column_name:'A.BASELOTNO', query_mode:'2', value: $scope.opmList.lot_no}
                    ],
                    header: [
                        {label: $filter('translate')('OPM01.queryList.lot_no'), code:'lot_no'},
                        {label: $filter('translate')('OPM01.queryList.product_no'), code:'product_no'},
                        {label: $filter('translate')('OPM01.queryList.product_name'), code:'product_name'}
                    ],
                    setCondition:function(conditions){
                        if($scope.opmList.product_no && $scope.opmList.wo_no){
                            conditions.push(
                                {column_name:'A.productno', query_mode:'2', value: $scope.opmList.product_no, merge_condition_model:'AND'},
                                {column_name:'A.MONo', query_mode:'2', value: $scope.opmList.wo_no, merge_condition_model:'AND'}
                            );
                        }
                        return conditions;
                    },
                    selectCode: $scope.opmList.lot_no || '',
                    confirm : function(selectItem){
                        $scope.opmList.lot_no = selectItem.lot_no;
                    }
                });
            };

            // 取得作業站編號資訊
            $scope.loadOpNo = function(){
                $MMWService.showQueryListPNIG({
                    page_mode: 'opm_op_no_info_get',
                    pageRecord: 50,
                    condition: [
                        {name:$filter('translate')('OPM01.queryList.op_no'), column_name:'C.OPNO', query_mode:'2', value: $scope.opmList.op_no}
                    ],
                    header: [
                        {label: $filter('translate')('OPM01.queryList.op_no'), code:'op_no'},
                        {label: $filter('translate')('OPM01.queryList.op_name'), code:'op_name'},
                        {label: $filter('translate')('OPM01.queryList.lot_no'), code:'lot_no'}
                    ],
                    setCondition:function(conditions){
                        if($scope.opmList.product_no && $scope.opmList.wo_no && $scope.opmList.lot_no){
                            conditions.push(
                                {column_name:'A.PRODUCTNO', query_mode:'2', value: $scope.opmList.product_no, merge_condition_model:'AND'},
                                {column_name:'A.MONo', query_mode:'2', value: $scope.opmList.wo_no, merge_condition_model:'AND'},
                                {column_name:'A.BASELOTNO', query_mode:'2', value: $scope.opmList.lot_no, merge_condition_model:'AND'}
                            );
                        }
                        return conditions;
                    },
                    selectCode: $scope.opmList.op_no || '',
                    confirm : function(selectItem){
                        $scope.opmList.op_no = selectItem.op_no;
                    }
                });
            };

            // 重置資料
            $scope.reset = function(){
                $scope.init();
            };

            // 送回資料
            $scope.confirm = function(){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_OPM.opm_list_process',
                    content : $scope.opmList,
                    success : function(data){
                        $scope.$root.showToast($filter('translate')('common.msg.success'));
                        $scope.init();
                    }
                });
            };

        }
    ];
});
